package ynu.edu.achitecturework.service;

import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ynu.edu.achitecturework.dao.CameraDataDAO;
import ynu.edu.achitecturework.dao.DeviceDAO;
import ynu.edu.achitecturework.dao.SensorDataDAO;
import ynu.edu.achitecturework.entity.CameraData;
import ynu.edu.achitecturework.entity.Device;
import ynu.edu.achitecturework.entity.SensorData;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DataCollectionService {

    private final FboxService fboxService;
    private final YsService ysService;
    private final DeviceDAO deviceDAO;
    private final SensorDataDAO sensorDataDAO;
    private final CameraDataDAO cameraDataDAO;

    public List<SensorData> collectSensorData(String boxNo) {
        List<Device> sensors = deviceDAO.findAll().stream()
                .filter(device -> !device.getDeviceType().equals("CAMERA"))
                .collect(Collectors.toList());

        List<String> sensorIds = sensors.stream()
                .map(Device::getSerialNumber)
                .collect(Collectors.toList());

        List<Map<String, Object>> sensorDataList = fboxService.getSensorData(boxNo, sensorIds);

        return sensorDataList.stream().map(data -> {
            SensorData sensorData = new SensorData();
            String serialNumber = (String) data.get("id");
            Device device = deviceDAO.findBySerialNumber(serialNumber);
            sensorData.setDevice(device);
            sensorData.setName((String) data.get("name"));

            // 处理不同类型的数值
            Object value = data.get("value");
            if (value instanceof Integer) {
                sensorData.setValue(((Integer) value).doubleValue());
            } else if (value instanceof Long) {
                sensorData.setValue(((Long) value).doubleValue());
            } else if (value instanceof String) {
                try {
                    sensorData.setValue(Double.parseDouble((String) value));
                } catch (NumberFormatException e) {
                    sensorData.setValue(0.0); // 或者抛出异常
                }
            } else if (value instanceof Double) {
                sensorData.setValue((Double) value);
            } else {
                sensorData.setValue(0.0); // 或者抛出异常
            }

            return sensorDataDAO.save(sensorData);
        }).collect(Collectors.toList());
    }

    public CameraData collectCameraData(Long deviceId) {
        String photoUrl = ysService.capturePhoto();
        if (photoUrl != null) {
            CameraData cameraData = new CameraData();
            cameraData.setDevice(deviceDAO.getReferenceById(deviceId));
            cameraData.setPhotoUrl(photoUrl);
            return cameraDataDAO.save(cameraData);
        }
        return null;
    }

    @Scheduled(fixedRate = 300000) // 每5分钟执行一次
    public void scheduledDataCollection() {
        collectSensorData("300023040198");
        Device camera = deviceDAO.findAll().stream()
                .filter(device -> device.getDeviceType().equals("CAMERA"))
                .findFirst()
                .orElse(null);
        if (camera != null) {
            collectCameraData(camera.getId());
        }
    }
}